package com.bytedance.scenedemo.extreme_case;

import com.bytedance.scene.Scene;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.interfaces.ChildSceneLifecycleCallbacks;
import com.bytedance.scene.interfaces.PushOptions;
import com.bytedance.scene.ktx.SceneExtensions;
import com.bytedance.scene.navigation.NavigationScene;
import com.bytedance.scenedemo.ResourceTable;
import com.bytedance.scenedemo.benchmark.BenchmarkSamples;
import com.bytedance.scenedemo.benchmark.performance.EmptyAppCompatAbility;
import com.bytedance.scenedemo.benchmark.performance.EmptyAppCompatScene;
import com.bytedance.scenedemo.lifecycle.EmptyScene100;
import com.bytedance.scenedemo.util.ResUtil;
import com.bytedance.scenedemo.utility.ColorUtil;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.IntentParams;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

/**
 * Created by JiangQi on 9/5/18.
 */
public class Case4Scene extends GroupScene implements ChildSceneLifecycleCallbacks{
    private StringBuilder stringBuilder = new StringBuilder();
    private Text textHint;
    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        return (ComponentContainer) inflater.parse(ResourceTable.Layout_case4_scene_layout,container,false);
    }

    @Override
    public void onViewCreated(Component layout, Intent intent) {
        super.onViewCreated(layout, intent);
        textHint = (Text) layout.findComponentById(ResourceTable.Id_text_case4scene_hint);
        DirectionalLayout svMain = (DirectionalLayout) layout.findComponentById(ResourceTable.Id_sv_push_pop_basic_usage_main);
        int value = 0;
        if(getArguments()!=null){
            value= (int) getArguments().getParam("value");
        }
        svMain.setBackground(ColorUtil.getMaterialColor(value));

        Button btnPushAndPop1 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_1);
        btnPushAndPop1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                IntentParams intentParams = new IntentParams();
                intentParams.setParam("type",1);
                intentParams.setParam("value",1);
                SceneExtensions.requireNavigationScene(Case4Scene.this).push(EmptyScene0.class,intentParams);
            }
        });
        Button btnPushAndMany2 = (Button) layout.findComponentById(ResourceTable.Id_case_push_pop_btn_2);
        btnPushAndMany2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                IntentParams intentParams = new IntentParams();
                intentParams.setParam("value",3);
                intentParams.setParam("type",0);
                SceneExtensions.requireNavigationScene(Case4Scene.this).push(EmptyScene2.class,intentParams);
            }
        });
        SceneExtensions.requireNavigationScene(Case4Scene.this).registerChildSceneLifecycleCallbacks(this,true);
    }

    private void ddd() {
        textHint.setText(stringBuilder.toString());
        new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
            @Override
            public void run() {
                textHint.scrollTo(0, 0);
            }
        }, 10);

    }

    @Override
    public void onActivityCreated(Intent intent) {
        super.onActivityCreated(intent);
    }

    @Override
    public void onSceneCreated(Scene scene, Intent intent) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onCreated");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneViewCreated(Scene scene, Intent intent) {

    }

    @Override
    public void onSceneActivityCreated(Scene scene, Intent intent) {

    }

    @Override
    public void onSceneStarted(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onStart");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneResumed(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onResume");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneSaveInstanceState(Scene scene, Intent intent) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onSaveInstanceState");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onScenePaused(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onPause");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneStopped(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onStop");
        stringBuilder.append("\n");
        ddd();
    }

    @Override
    public void onSceneViewDestroyed(Scene scene) {

    }

    @Override
    public void onSceneDestroyed(Scene scene) {
        String content = scene.toString();
        String str = content.substring(0, content.indexOf('{'));
        stringBuilder.append(str + " onDestroy");
        stringBuilder.append("\n");
        ddd();
    }
}